$(function () {
    /*
     * 抽取所有需要用得元素.
     */
    var transferBillDatagrid,transferBillSearchBtn;
    transferBillDatagrid = $("#transferBill_datagrid");
    transferBillSearchBtn = $("#searchBtn");
    /*
     * 初始化数据表格
     */
    transferBillDatagrid.datagrid({
        url: "/transferBill/list",
        fit: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        fitColumns: true,
        toolbar: '#transferBill_datagrid_tb',
        columns: [
            [
                {field: 'client', width: 10, align: 'center', title: '被移交的客户',formatter: function (value, row, index) {
                    return value ? value.realname:"";
                }},
                {field: 'transfer', width: 10, align: 'center', title: '移交人',formatter: function (value, row, index) {
                    return value ? value.realname:"";
                }},
                {field: 'receiver', width: 10, align: 'center', title: '接收人',formatter: function (value, row, index) {
                    return value ? value.realname:"";
                }},
                {field: 'transferofreason', width: 10, align: 'center', title: '移交原因'},
                {field: 'transfertime', width: 10, align: 'center', title: '移交时间'}
            ]
        ]
    });
    transferBillSearchBtn.textbox({
        width:230,
        label:"关键字:",
        labelWidth:50,
        prompt:"请输入搜索关键字",
        buttonText:'搜索',
        buttonIcon:'icon-search',
        onClickButton:function(){
            var keyword = $(this).val();
            transferBillDatagrid.datagrid("load",{
                keyword:keyword
            });
        }
    });
    /*
     * 对页面按钮进行统一监听
     */
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    /*
     * 所有的操作封装到cmdObj对象中,方便管理
     */
    var cmdObj = {
        del: function () {
            var rowData = transferBillDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要删除选中数据吗？", function (yes) {
                    if (yes) {
                        $.get("/transferBill/delete?transferBillId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    transferBillDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要删除的数据!", "warining");
            }
        },
        //刷新,确认,取消
        reload: function () {
            transferBillDatagrid.datagrid("reload");
        }
    }
});
